package com.power.model.factories
{
	import com.power.model.RoomVo;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.controls.Image;

	public class RoomFactory extends EventDispatcher
	{
		private var _config_url:String;
		
		private var _rooms:Vector.<RoomVo>= new Vector.<RoomVo>();
		
		private var _xmlloader:URLLoader;
		
		private var _roomxml:XML;
		
		private var _roompicurls:Array =[];
		
		private var _roomtypes:Array = [];
		
		private var _roomtotalnum:int;
		
		private var _roomindex:int = 0 ;
		
		private var _temptype:String;
		
		private var _roomTagsUrl:Array=[];
		
		private var _roomImage:Bitmap;
	
		private var _roomTagImage:Bitmap;
		
		private var _roomType:String;
		
		public function RoomFactory()
		{
			
		}
		public function startLoadRoomAssets(roomurl:String,roomType:String):void
		{
			_config_url = roomurl;
			_roomType = roomType;
			_xmlloader = new URLLoader();
			_xmlloader.load(new URLRequest(_config_url));
			_xmlloader.addEventListener(ProgressEvent.PROGRESS,loadroomxmlProHandler);
			_xmlloader.addEventListener(Event.COMPLETE,loadxmlendhandler);
		}
		private function loadroomxmlProHandler(e:ProgressEvent):void
		{
			// indicator progress when start to loaded room.xml file
		}
		private function loadxmlendhandler(e:Event):void
		{
			var tempxml:XML = new XML(e.target.data);
			startParseXml(tempxml);
		}
		private function startParseXml(xml:XML):void
		{
			_roomxml = xml;
			_roomtotalnum = _roomxml.room.length();
			for(var i:int = 0 ;i<_roomtotalnum;i++)
			{
				var tempurl:String = _roomxml.room[i].picurl;
				var temptype:String = _roomxml.room[i].type;
				var temptag:String = _roomxml.room[i].tag;
				if(_roomType==temptype)
				{
					_roompicurls.push(tempurl);
					_roomtypes.push(temptype);
					_roomTagsUrl.push(temptag);
				}
				if(_roomType=="全部房间")
				{
					_roompicurls.push(tempurl);
					_roomtypes.push(temptype);
					_roomTagsUrl.push(temptag);
				}
			}
			loadRoomData();
		}
		private function loadRoomData():void
		{
			var tempurl:String  = _roompicurls[_roomindex];
			_temptype = _roomtypes[_roomindex];
			var temploader:Loader = new Loader();
			try{
				temploader.load(new URLRequest(tempurl));
				temploader.contentLoaderInfo.addEventListener(Event.COMPLETE,onloaderEndHandler);
			}catch(e:Error){
				
				throw new Error("the url of picture you improve is incorrect!");
			}
			
				
		}
		private function onloaderEndHandler(e:Event):void
		{
			_roomImage = new Bitmap(e.currentTarget.content.bitmapData);
			var tempurl:String = _roomTagsUrl[_roomindex];
			var tagloader:Loader = new Loader();
			tagloader.load(new URLRequest(tempurl));
			tagloader.contentLoaderInfo.addEventListener(Event.COMPLETE,onloaderTagEndHandler);
			
		}
		private function onloaderTagEndHandler(e:Event):void
		{
			_roomTagImage = new Bitmap(e.currentTarget.content.bitmapData);
			var roomdata:RoomVo = new RoomVo();
			roomdata._image =_roomImage;
			roomdata._tag = _roomTagImage;
			roomdata._type = _temptype;
			_rooms.push(roomdata);
			_roomindex++;
			if(_roomindex>=_roompicurls.length)
			{
				this.dispatchEvent(new Event("ParserRoomDataEnd",true));
				this.dispatchEvent(new Event("ParserRoomDataEndByType",true));
			}else
			{
				loadRoomData();
			}
			
		}
		public function getRoomData():Vector.<RoomVo>
		{
			return _rooms;
		}
		
	}
}